﻿using System.Collections.Generic;
using System.Linq;
using QuocViet.WebUI.Models.Interface;
using Project.Domain.Entities;
using Framework.EntityFramework;

namespace QuocViet.WebUI.Models.Repository
{
    public class SportRepository:ISportRepository
    {
        private readonly DomainContext _db = new DomainContext();

        public IEnumerable<Bet> Getbetbymatchid(int matchid)
        {
            return _db.Bets
                .Where(b => b.MatchId == matchid && b.Active == false)
                .OrderBy(b => b.Type);
        }             

        //get all league has status =true(it's has match in play):used
        public IEnumerable<League> GetAllLeague()
        {
            return _db.Leagues
                .Where(l => l.IsPlay);
        }

        //list all match has status=true(true=not start) Used
        public IEnumerable<Match> GetAllMatchByLeagueId(int leagueid)
        {
            return _db.Matches
                .Where(m => m.LeagueId == leagueid && m.Status)
                .OrderBy(m => m.Status)
                .ThenBy(m => m.LeagueId);
        }
        
        public IEnumerable<BetDetail> GetBetDetailsByBetId(int betid,int UserId)
        {
            return _db.BetDetails
                .Where(b => b.BetId == betid && b.Bet.UserId == UserId)
                .OrderBy(b => b.BetId);                
        }

        public IEnumerable<BetDetail> GetAllBetExactScoreOfMatch(int matchid,int UserId)
        {
            return _db.BetDetails
                .Where(b => b.Bet.MatchId == matchid && b.Bet.Type == 5 && b.Bet.UserId == UserId);
        }

        //get all userbet of match have id: Used
        public IEnumerable<Bet> GetAllUserBetOfMatchId(int matchid)
        {
            var listuserBet = from user in _db.Bets
                              where user.MatchId == matchid
                              group user by user.UserId into uniqueUserIds
                              select uniqueUserIds.FirstOrDefault();
            return listuserBet;
        }     
    }
}